<?php

namespace app\admin\model;

//加载配置类
use think\Config;
//导入Controller
use think\Model;
//导入Db
use think\Db;
//引入Session类
use think\Session;

use app\admin\model\JsonModel;

class LoginModel extends Model
{
	/**
	*	后台管理员登录
	*/
	public function getAdminUser($data)
	{	
		$where = [];
		$where['username'] = $data['username'];
		$JsonModel = new JsonModel();
		$ip = $JsonModel->get_client_ip(0, true);
		$map = [
			'last_login_time' => strtotime(date('Y-m-d h:i:s', time())),
			'last_login_ip' => $ip
		];
		// md5加密
		$pass = md5($data['password']);
		if(!empty($data)){
			$res = Db::table('sz_adminuser')->where($where)->select();
			Db::table('sz_adminuser')->where('id',$res[0]['id'])->update($map);
			if(!empty($res)){
				if($pass == $res[0]['password']){

					if($res[0]['status'] == 1){
					
						Session::set('admin_id',$res[0]['id']);
						// 登录成功
					    return 0;
					}else{
						// 该账号已经被禁用
						return 1;
					}

				}else{

					//密码错误
					return 2;
				}
			}else{

				//账号不存在
				return 3;
			}
		}
	}
}